package com.xkcyy.study.spring.d02_aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * LogAdvisor
 *
 * @author yuand
 * @date 2021/6/3
 **/
@Component
// todo 说明为什么要添加Role
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class LogPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor implements MethodInterceptor {
    public LogPointcutAdvisor() {
        this.setAdvice(this);
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        Log annotation = method.getAnnotation(Log.class);
        return annotation != null;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("执行了" + LogPointcutAdvisor.class + "拦截器");
        return invocation.proceed();
    }

}
